package com.vitabuybuy.tv.ui.mine.talent

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityFieldLabelBinding
import com.vitabuybuy.tv.dto.FieldLabelDto
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.toast.XToast
import com.library.common.widget.RecycleViewDivider
import com.vitabuybuy.tv.ui.mine.talent.adapter.FieldLabelAdapter
import com.vitabuybuy.tv.ui.mine.talent.viewmodel.TalentApplyViewModel
import kotlinx.android.synthetic.main.activity_field_label.*

class FieldLabelActivity : BaseActivity<TalentApplyViewModel, ActivityFieldLabelBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_field_label

    private var fieldLabellist: MutableList<FieldLabelDto>? = null

    private val labelAdapter by lazy { FieldLabelAdapter() }

    override fun onGetBundle(bundle: Bundle?) {
        fieldLabellist = bundle?.getSerializable("fieldLabellist") as ArrayList<FieldLabelDto>
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.field_label))
        mTvRight.apply {
            text = getString(R.string.save)
            visibility = View.VISIBLE
        }
        mBinding.mViewModel = viewModel
        listview.apply {
            adapter = labelAdapter
            layoutManager = LinearLayoutManager(mContext)
            addItemDecoration(RecycleViewDivider(mContext, 1))
        }
    }

    override fun observeData() {
        viewModel.getFieldLabelList()
        viewModel.apply {
            labellist.observe(this@FieldLabelActivity, Observer {
                if (fieldLabellist.isNullOrEmpty()) {
                    labelAdapter.addData(it)
                    return@Observer
                }
                fieldLabellist?.run {
                    for (lable in this) {
                        for (lable2 in it) {
                            if (lable2.id == lable.id) {
                                lable2.isChecked = true
                            }
                        }
                    }
                    labelAdapter.addData(it)
                }
            })
        }
    }

    override fun eventHandling() {
        labelAdapter.setOnItemClickListener { _, _, position ->
            labelAdapter.mData[position].isChecked = !labelAdapter.mData[position].isChecked
            labelAdapter.notifyItemChanged(position)
        }
        mTvRight.setOnClickListener {
            val labellist = ArrayList<FieldLabelDto>()
            for (bean in labelAdapter.mData) {
                if (bean.isChecked) {
                    labellist.add(bean)
                }
            }
            if (labellist.size > 5) {
                XToast.normal(getString(R.string.most_five_field_label))
                return@setOnClickListener
            }
            val intent = Intent()
            intent.putExtras(bundleOf(Pair("labelList", labellist)))
            finishResult(intent)
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                viewModel.getFieldLabelList()
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, fieldLabellist: ArrayList<FieldLabelDto>) {
            mContext.startForResult(
                bundleOf(Pair("fieldLabellist", fieldLabellist)),
                10001,
                FieldLabelActivity::class.java
            )
        }
    }
}
